import { HttpClient } from '@angular/common/http';
import { ChangeDetectionStrategy, Component, Input, OnInit } from '@angular/core';
import { NzTypographyComponent } from 'ng-zorro-antd/typography';

import { Api } from '../api';

@Component({
  selector: 'app-preview-text',
  template: `<p nz-typography [nzContent]="textSrc"> </p>`,
  changeDetection: ChangeDetectionStrategy.Default,
  standalone: true,
  imports: [NzTypographyComponent]
})
export class TextPreviewComponent implements OnInit {
  @Input() record: any = {};

  textPreviewUrl: string = `${Api.BasePreviewApi}/`;

  textSrc: string = '';
  constructor(private httpClient: HttpClient) {}

  ngOnInit(): void {
    if (this.record && this.record.id) {
      const url = this.textPreviewUrl + this.record.id;
      this.httpClient.get(url, { responseType: 'blob' }).subscribe(blob => {
        blob.text().then(res => {
          // TODO 排版
          this.textSrc = res;
        });
      });
    }
  }
}
